package com.biglead.mini.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.mini.mapper.OmsCartItemMapper;
import com.biglead.mini.pojo.CartPromotionItem;
import com.biglead.mini.pojo.OmsCartItem;
import com.biglead.mini.service.OmsCartItemService;
import com.biglead.mini.service.OmsPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class OmsCartItemServiceImpl extends ServiceImpl<OmsCartItemMapper, OmsCartItem> implements OmsCartItemService {


    @Autowired
    private OmsPromotionService omsPromotionService;
    @Override
    public boolean add(OmsCartItem cartItem, Long userId) {

        boolean count = false;
        cartItem.setMemberId(userId);
        cartItem.setDeleteStatus(0);
        OmsCartItem existCartItem = getCartItem(cartItem);
        if (existCartItem == null) {
            cartItem.setCreateDate(new Date());
            count = this.save(cartItem);
        } else {
            cartItem.setModifyDate(new Date());
            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
            count = this.updateById(existCartItem);
        }
        return count;
    }

    @Override
    public List<OmsCartItem> list(Long userId) {
        List<OmsCartItem> cartItemList = this.list(new LambdaQueryWrapper<OmsCartItem>()
                .eq(OmsCartItem::getMemberId, userId)
                .eq(OmsCartItem::getDeleteStatus, 0)
        );

        return cartItemList;
    }

    @Override
    public List<CartPromotionItem> listPromotion(Long userId, List<Long> cartIds) {
        List<OmsCartItem> cartItemList = list(userId);
        if (!cartIds.isEmpty()) {
            cartItemList = cartItemList.stream().filter(item -> cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(cartItemList)) {
            cartPromotionItemList = omsPromotionService.calcCartPromotion(cartItemList);
        }
        return cartPromotionItemList;
    }

    @Override
    public boolean updateQuantity(Long id, Long userId, Integer quantity) {

        OmsCartItem one = this.getOne(new LambdaQueryWrapper<OmsCartItem>()
                .eq(OmsCartItem::getId, id)
                .eq(OmsCartItem::getDeleteStatus, 0)
                .eq(OmsCartItem::getMemberId, userId)
        );
        if (one != null) {
            one.setQuantity(quantity);
            return this.updateById(one);
        }
        return false;
    }

    @Override
    public boolean clear(Long userId) {
        List<OmsCartItem> one = this.list(new LambdaQueryWrapper<OmsCartItem>()
                .eq(OmsCartItem::getMemberId, userId)
        );
        if (!one.isEmpty()) {
            for (OmsCartItem omsCartItem : one) {
                omsCartItem.setDeleteStatus(1);
            }
            return this.updateBatchById(one);
        }
        return false;
    }

    @Override
    public boolean delete(Long userId, List<Long> ids) {
        List<OmsCartItem> one = this.list(new LambdaQueryWrapper<OmsCartItem>()
                .eq(OmsCartItem::getMemberId, userId)
                .in(OmsCartItem::getId, ids)
        );
        if (!one.isEmpty()) {
            for (OmsCartItem omsCartItem : one) {
                omsCartItem.setDeleteStatus(1);
            }
            return this.updateBatchById(one);
        }
        return false;
    }


    /**
     * 根据会员id,商品id和规格获取购物车中商品
     */
    private OmsCartItem getCartItem(OmsCartItem cartItem) {

        List<OmsCartItem> cartItemList = this.list(new LambdaQueryWrapper<OmsCartItem>()
                .eq(OmsCartItem::getMemberId, cartItem.getMemberId())
                .eq(OmsCartItem::getProductId, cartItem.getProductId())
                .eq(cartItem.getProductSkuId() != null, OmsCartItem::getProductSkuId, cartItem.getProductSkuId())
                .eq(OmsCartItem::getDeleteStatus, 0)
        );
        if (!CollectionUtils.isEmpty(cartItemList)) {
            return cartItemList.get(0);
        }
        return null;
    }
}
